package com.paireach.bw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.bean.ExceptionInfoBean;

import java.util.List;

/**
 * Created by ${cqc} on 2017/6/9.
 * 首页第3个tab：预警列表 adapter
 */

public class ExceptionListAdapter extends RecyclerView.Adapter {

    private List<ExceptionInfoBean> list;

    public ExceptionListAdapter(List<ExceptionInfoBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.item_exception_info, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ExceptionInfoBean info = list.get(position);
        MyViewHolder holder = (MyViewHolder) viewHolder;
        holder.tv_order_code.setText("负载单号：" + info.getOrderCode());
        holder.tv_state_name.setText(info.getStatusName() + "");
        holder.tv_msg.setText(info.getStartTime() + "开始，在" + info.getRegions() + "停留" + info.getStopTime() + "分钟");

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
        }

        AnimationSet set = new AnimationSet(true);
//        TranslateAnimation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, -1.0f, TranslateAnimation.RELATIVE_TO_SELF, 0.0f, TranslateAnimation.RELATIVE_TO_SELF, 0.0f, TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
        ScaleAnimation scale = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        set.addAnimation(scale);
//        set.addAnimation(translate);

        set.setDuration(500);
        holder.itemView.startAnimation(set);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_order_code;
        TextView tv_state_name;
        TextView tv_msg;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_order_code = (TextView) itemView.findViewById(R.id.tv_order_code);
            tv_state_name = (TextView) itemView.findViewById(R.id.tv_state_name);
            tv_msg = (TextView) itemView.findViewById(R.id.tv_msg);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
